import os
import os.path

class _Config(object):

    base = os.path.expanduser('~/projects/activist.ca')
    base = os.path.abspath(os.curdir)

    is_beta = False

    theme = 'modern'

    munge_ip_logging = False

    events_per_page = 20

    seconds_per_checkpoint = 3600*24    # every day

    email_host = 'localhost'

    email_errors_to = ['mike@meejah.homeip.net']

    max_email_addresses = 5

    recurring_upcoming_days_to_show = 4

    categories = ['category a', 'category b']

    inlineNegativeAccessibleKeys = ['smoking']

    shortenCalendarTitles = False

    dataPath = os.path.join(base,'data')
    templatePath = os.path.join(base,'templates/%s'%theme)

    debug = False

    STATIC_NODES = ['highlighted events',
                    'moderated',
                    'administrator',
                    'moderator',
                    'commentator',
                    ]

    def template(self,rest):
        return os.path.join(self.templatePath,rest)

Config = _Config()



def bootstrapInitialize():
    """
    This is called when the CommandProcessor starts up and finds that
    there are no commands -- that is, there will be no objects created
    into the DataStore. This does initial creation (after a "make clean
    && make restore" for example).
    """

    print "BOOTSTRAP initial data."
    
    from activist.GenericData import GenericData
    from activist.Category import Category
    from activist.Person import Person

    for key in Config.STATIC_NODES:
        a = GenericData()
        a.create(key)

    for key in Config.categories:
        a = Category()
        a.create(key)

    from activist.Article import Article
    from activist.Alias import Alias
    from activist.Media import Media
    from activist.Text import Text
    def recursivelyCreateDocs(path,tree):
        for doc in os.listdir(path):
            if doc == '.svn':
                continue
            
            p = os.path.join(path,doc)
            if doc != 'index':
                mytree = tree + [doc]
            else:
                mytree = tree
            if os.path.isdir(p):
                recursivelyCreateDocs(p, mytree)
            else:
                print "creating",doc,p,mytree
                data = open(p,'r').readlines()
                if not len(data):
                    #print "  empty lines!"
                    continue
                
                links = data[0]
                up = None
                next = None
                prev = None
                title = doc
                if links.find(':') >= 0:
                    for x in links.split(','):
                        (n,v) = x.split(':')
                        n = n.strip().lower()
                        v = v.strip()
                        #print "N,V",n,v
                        if n == 'up': up = v
                        if n == 'next': next = v
                        if n == 'prev': prev = v
                        if n == 'title': title = v
                data = '\n'.join(data[1:])
                text = Text()
                text.create(data)
                article = Article()
                article.create(title,text,creator=None,
                               up=up, next=next, prev=prev)
                alias = Alias()
                alias.create('/'+'/'.join(mytree),'/article/' + article.id())
                #print article,alias,'/'.join(mytree)
    recursivelyCreateDocs(os.path.join(Config.base,'web-documentation'), [])

    if os.path.isfile(os.path.join(Config.templatePath,"MEDIA")):
        data = open(os.path.join(Config.templatePath,"MEDIA"),'r').readlines()
        for line in data:
            line = line.strip()
            if len(line) == 0: continue
            if line[0] == '#': continue

            stuff = line.split()
            mimetype = None
            if len(stuff) == 3:
                (file,alias,mimetype) = stuff
            else:
                (file,alias) = stuff

            media = Media()
            media.create(file,open(os.path.join(Config.templatePath,file),'r').read(),mimetype=mimetype)
            thealias = Alias()
            thealias.create(alias,'/media/' + str(media.id()))

            print "created media:",media




                                    # need an admin

    admin = Person()
    admin.create('Administrator', 'admin', 'admin', '', 'admin@activist.ca', None, "This is the generic administrator account and shouldn't be used for day-to-day administration purposes; make other user accounts and grant them Administrator privileges using this account.", False)
    admin.make_administrator(admin,True)
    admin.make_moderator(admin,True)


    from activist.util import sendMessageToAllAdministrators
    sendMessageToAllAdministrators("Activist.ca started with no data; initialised default data.")
